package com.dongjiantong.user.serviceImpl;/**
 * Created by cao on 2017/11/13.
 */

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.user.enums.OrgTypeEnum;
import com.dongjiantong.user.mapper.MzUserMapper;
import com.dongjiantong.user.mapper.OrganizationMapper;
import com.dongjiantong.user.mapper.OrganizationUserMapper;
import com.dongjiantong.user.model.*;
import com.dongjiantong.user.enums.UserEnum;
import com.dongjiantong.user.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cao
 * @create 2017-11-13 下午3:50
 **/
@Slf4j
@Service(version = "1.0.0")
public class MzUserServiceImpl extends ServiceMybatis<MzUser> implements MzUserService {

    @Resource
    private MzUserMapper mzUserMapper;

    @Autowired
    private SSOLoginService ssoLoginService;
    @Resource
    private OrganizationUserMapper organizationUserMapper;
    @Resource
    private OrganizationMapper organizationMapper;
    @Resource
    private FarmsUserService farmsUserService;
    @Resource
    private RoleAppUserService roleAppUserService;


    @Override
    public MzUser getMzUser(long keyId) {
        return mzUserMapper.selectByPrimaryKey(keyId);
    }


    @Override
    public Response<MzUser> checkLogin(String username, String password, UserEnum userEnum) {

        Response<LoginUser> loginResult = ssoLoginService.doLogin(username, password);

        //登录不成功
        if (!loginResult.isSuccess()) {
            return Response.fail(HttpCode.LOGIN_FAIL, loginResult.getMessage());
        }

        //登录成功
        /**
         * 1. 查询人员信息
         */
        MzUser mzUser = mzUserMapper.selectByPrimaryKey(loginResult.getResult().getUserId());

        if (userEnum == UserEnum.LOGIN_APP) {
            //app登录
            if (mzUser.getUserType() == UserEnum.USERTYPE_BOSS.getCode()) {
                return Response.fail(HttpCode.FORBIDDEN, "该帐号不充许登录APP");
            }


        } else if (userEnum == UserEnum.LOGIN_WEB) {
            //web登录
            if (mzUser.getUserType() == UserEnum.USERTYPE_BOSS.getCode()) {
                return Response.fail(HttpCode.FORBIDDEN, "该帐号不充许登录web平台");
            }

        } else if (userEnum == UserEnum.LOGIN_BOSS) {
            //boss登录
            if (mzUser.getUserType() != UserEnum.USERTYPE_BOSS.getCode()) {
                return Response.fail(HttpCode.FORBIDDEN, "该帐号不充许登录boss平台");
            }

        }

        return Response.ok(mzUser);
    }


    @Override
    public MzUser queryAdminAccount(long regionId) {
        Example examplePro = new Example(MzUser.class);
        Example.Criteria criteriaPro = examplePro.createCriteria();

        criteriaPro.andEqualTo("relationId", regionId);
        criteriaPro.andEqualTo("ismaster", 1L);

        List<MzUser> list = mzUserMapper.selectByExample(examplePro);

        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public PageInfo selectByUserIdList(List<Long> userIdList, String userName, String phone, Integer role, Integer pageNum, Integer pageSize) {

        List<Long> userId=userIdList;
        if (role != null) {
            userId= roleAppUserService.selectByRoleAndUserList(role, userIdList);
        }

        Example example = new Example(MzUser.class);
        userId.add(-1L);
        example.createCriteria().andIn("id", userId)
                .andLike("mobile", "%" + phone + "%")
                .andLike("realname", "%" + userName + "%");
        example.setOrderByClause("created_at desc");
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<MzUser> pageInfo = new PageInfo(mzUserMapper.selectByExample(example));
        for (MzUser mzUser : pageInfo.getList()) {
            OrganizationUser organizationUser = new OrganizationUser();
            organizationUser.setUserId(mzUser.getId());
            List<OrganizationUser> organizationUserList = organizationUserMapper.select(organizationUser);
            String depName = "";
            String orgName = "";
            for (OrganizationUser organizationUser1 : organizationUserList) {

                if (organizationUser1.getType() == OrgTypeEnum.OrgType_organization.getCode()) {
                    orgName = organizationMapper.selectByPrimaryKey(organizationUser1.getOrgId()).getName();
                } else {
                    depName = organizationMapper.selectByPrimaryKey(organizationUser1.getOrgId()).getName();
                }
            }
            String farmName = "";
            Farms farms = farmsUserService.getFarmByUserId(mzUser.getId());
            if (farms != null) {
                farmName = farms.getFarmsname();
            }

            mzUser.setFarmName(farmName);
            LoginUser loginUser = new LoginUser();
            loginUser.setUserId(mzUser.getId());
            LoginUser loginUser1 = ssoLoginService.selectOne(loginUser);
            if (loginUser1 != null) {
                Integer status = loginUser1.getStatus();
                mzUser.setLoginType(status);
            }

            mzUser.setOrgName(orgName);
            mzUser.setDepName(depName);
        }
        return pageInfo;
    }

}
